<template>
  <el-row >
    <el-col :span="leftColSpan" style="margin-bottom: 20px">
      <div class="app-container">
        <!-- 查询区域 -->
        <el-form :model="queryParams" :ref="queryFormRef" inline v-show="showSearch" label-width="auto" @keyup.enter.native="handleQuery">
          <el-form-item label="关键字" prop="keywords" style="width: 220px;">
            <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 150px;"/>
          </el-form-item>
          <el-form-item label="状态" prop="status" label-width="40px" style="width: 170px;">
            <lam-dict-select v-model="queryParams.status" dictCode="status_type" placeholder="状态" clearable style="width: 120px;"/>
          </el-form-item>
          <el-form-item label-width="0px">
            <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="tool-bar-btn">
          <el-button type="primary" icon="el-icon-plus"  @click="handleAdd" v-hasPerms="['sys:role:add']">新增</el-button>
          <el-button type="success" icon="el-icon-edit"  :disabled="single" @click="handleUpdate" v-hasPerms="['sys:role:edit']">修改</el-button>
          <el-button type="info" icon="el-icon-upload2"  @click="handleImport" v-hasPerms="['sys:role:export']">导入</el-button>
          <el-button type="danger" icon="el-icon-delete"  :disabled="multiple" @click="handleDelete" v-hasPerms="['sys:role:remove']">删除</el-button>
          <el-button type="warning" icon="el-icon-download"  @click="handleExport" v-hasPerms="['sys:role:export']">导出</el-button>
          <el-button icon="el-icon-close" @click="closeUserHandler" v-if="rightColSpan"  class="close-user-bt" :style="closeUserStyle">关闭用户</el-button>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>

        <selected-info :count="selectedRows.length" :onClearSelected="onClearSelected" />
        <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
              :default-sort = "defaultSort" @sort-change="onSortChange" :row-key="rowKey" highlight-current-row >
          <el-table-column type="selection" width="55" align="center" fixed="left"/>
          <el-table-column label="编号" align="center" prop="id" sortable="custom" width="80"/>
          <el-table-column label="角色名称" align="center" prop="roleName"  width="180" />
          <el-table-column label="角色编码" align="center" prop="roleCode"  width="180" />
          <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="statusDictText" width="80">
            <template slot-scope="{row}"><LamStatus :value="row.status" :text="row.statusDictText"/></template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="createBy"  width="120" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
          <el-table-column label="修改人" align="center" prop="updateBy"  width="120" />
          <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
          <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="200">
            <template slot-scope="{row}">
              <el-button type="text" icon="el-icon-user" @click="handleUser(row)" v-hasPerms="['sys:user:toSelectList:roleUser']">用户</el-button>
              <el-button type="text" icon="el-icon-key" @click="openRoleMenu(row, 1)" v-hasPerms="['sys:roleMenu:list']">授权</el-button>
              <!-- <el-button type="text" icon="control" @click="openRoleMenu(row, 2)" v-hasPerms="['sys:sysRole:menu']">前端授权</el-button> -->
              <el-dropdown v-hasPerms="['sys:role:edit','sys:role:remove']">
                <el-link type="primary" icon="el-icon-arrow-down" :underline="false" class="el-button--small">更多</el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-hasPerms="['sys:role:edit']">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)">修改</el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-hasPerms="['sys:role:remove']">
                     <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <EditRole ref="formDialog" @ok="getList" />

        <!-- 导入对话框 -->
        <ToImport ref="importDialog" @ok="getList"/>
        <!-- 导出对话框 -->
        <ToExport ref="exportDialog" />

        <!-- 角色权限菜单列表 -->
        <RoleMenuDrawer ref="roleMenuDrawer" />

      </div>
    </el-col>
    <el-col :span="rightColSpan">
      <!-- 角色用户列表 -->
      <RoleUserList  ref="roleUserList" :roleId="curRow.id" :roleName="curRow.roleName"/>
    </el-col>

  </el-row>
</template>

<script>
import {httpGet, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import EditRole from './edit';
import ToImport from '@/components/upload/ToImport';
import ToExport from '@/components/download/ToExport';
import RoleUserList from './roleUserList';
import RoleMenuDrawer from './roleMenuDrawer';
import {hexToRGB} from '@/utils/cssUtil'

export default {
  name: "Role",
  mixins:[IndexListMixin],
  components: {
    EditRole,
    ToImport,
    ToExport,
    RoleUserList,
    RoleMenuDrawer,
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        keywords: null,
        status: null,
        pageNum: 1,
        pageSize: 10
      },
      url: {
      	exportXls: '/sys/role/export',
        base: '/sys/role/',
        downloadTemplateUrl: '/sys/role/downloadTemplate',
        uploadUrl: '/sys/role/importData',
      	list: '/sys/role/list'
      },
      roleUserShow: false,
      curRow:{}
    };
  },
  created() {
    this.getList();
  },
  computed:{
    leftColSpan() {
      return this.roleUserShow ? 14 : 24
    },
    rightColSpan() {
      return this.roleUserShow ? 10 : 0
    },
    closeUserStyle(){
      return {
        'background-color': hexToRGB(this.$store.state.settings.theme, 30)
      }
    }
  },
  mounted(){
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    /** 用户按钮操作 */
    handleUser(row) {
      this.curRow = row;
      this.roleUserShow = true;
    },
    /** 关闭用户操作 */
    closeUserHandler() {
      this.curRow = {};
      this.roleUserShow = false;
    },

    /** 授权按钮操作 */
    openRoleMenu(row, type){
      this.$refs.dataTable.clearSelection()
      this.$refs.dataTable.toggleRowSelection(row, true)
      this.$refs.roleMenuDrawer.show(row.id, type, row.roleName);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.formDialog.toAdd("添加角色配置");
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate("修改角色配置", httpGet(this.url.base + id));
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selectedRowKeys
      this.deleteData(this.url.base, ids);
    },

    /** 导入按钮操作 */
    handleImport() {
      this.$refs.importDialog.openDialog({
        title: '角色配置导入',
        uploadUrl: this.url.uploadUrl,
        downloadTemplateUrl: this.url.downloadTemplateUrl
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.exportData("角色配置导出", this.url.exportXls, this.queryParams)
    }
  }
};
</script>

<style scoped>
  .close-user-bt{
    position: relative;
    float: right;
  }
</style>
